//
//  SMChildRecomendController.swift
//  STMusic
//
//  Created by stoneobs on 17/3/21.
//  Copyright © 2017年 stoneobs. All rights reserved.
//
//**********************话题***************************
import UIKit

class YYChildTopicViewController: STTableViewController {
    
    var adverScrollView:STAdvertingScrollView?;
    
    var adverArray:Array<String>?;
    
    var dataSouce:Array<YYChildTopicModel> = [];
    
    var page:Int = 1;
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.loadNetworkRequest();
        self.view.backgroundColor = YYThemeStyleManger.defultManger.gradualColor(frame: self.view.bounds);
        self.tableView.separatorStyle = .none;
        self.tableView.backgroundColor = YYThemeStyleManger.defultManger.gradualColor(frame: self.tableView.bounds);
        self.tableView.frame = .init(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - 44 - 64);
        initSbviews();
        
        
        //UIViewController;
        // Do any additional setup after loading the view.
    }
    
    override init!(style: UITableViewStyle) {
        super.init(style: style);
    }
    
    
    override init!() {
        super.init();
        
    }
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // MARK:privite Method
    func initSbviews() -> Void {
        //刷新
         weak var weakSelf = self;
        self.tableView.mj_header = MJChiBaoZiHeader.init(refreshingBlock: {
            weakSelf?.page = 1;
             weakSelf?.dataSouce = [];
             weakSelf?.loadNetworkRequest();
            
        });

        self.tableView.mj_footer = MJChiBaoZiFooter.init(refreshingBlock: {
             weakSelf?.page  = self.page + 1;
             weakSelf?.loadNetworkRequest();
        });
        
        
        
        
    }
    
    // MARK:Network Request
    func loadNetworkRequest() -> Void {
        YYSuiYueNetWorkManger.defultManger.getNetwork(urlString: YYHomePageTopicUrl, dic: ["lang":"utf8","page":self.page], successHandle: { (json) in
            
            print("json = \(json)");
            
            let dataDic:NSDictionary? = json["data"] as? NSDictionary ;
            let array:Array<NSDictionary>! = dataDic?["list"] as! Array<NSDictionary>;
            for i in 0...array!.count - 1{
                if i != 0 {
                    let model = YYChildTopicModel.mj_object(withKeyValues: array[i]);
                    self.dataSouce.append(model!);
                    
                }
                
                
            }

            self.tableView.mj_header.endRefreshing();
            self.tableView.mj_footer.endRefreshing();
            self.tableView.reloadData();
            
            
        }) { (str) in
            self.tableView.mj_header.endRefreshing();
            self.tableView.mj_footer?.endRefreshing();
        };
    }
    // MARK: UITableView DataSouce
    override func numberOfSections(in tableView: UITableView) -> Int {
        return self.dataSouce.count;
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1;
    }
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return YYChildTopicTableViewCellHeight;
    }
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 15;
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell:YYChildTopicTableViewCell? = tableView.dequeueReusableCell(withIdentifier: "cell") as? YYChildTopicTableViewCell;
        if cell == nil {
            cell = YYChildTopicTableViewCell.init(style: .default, reuseIdentifier: "cell");
        }
        if self.dataSouce.count > 0 {
            cell?.model = self.dataSouce[indexPath.section];
        }
        weak var weakSelf = self;
        cell?.didClicCell = {[weak cell] in
            //之后会增加动画
        weakSelf?.navigationController?.pushViewController(YYTopicDetailViewController.init(topicModel: (cell?.model.topic)!), animated: true);
        }
        
        return cell!;
    }
    
    // MARK: UITableView Delegate
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: false);

    }
    
}
